#!/usr/bin/env python3
# -*- Mode: Python3; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-

# pongcli - Command-line Pong
# Copyright 2013 Elias Ylönen <elias.ylonen@gmail.com> and Veikka Halonen <veikka.halonen@iki.fi>

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#

import sys, time, curses
from curses import wrapper

def main(scr):
	curses.curs_set(0)
	ball = [10, 10] # y, x
	vector = [-15, 15] # y, x
	t = time.time()
	while True:
		dt = time.time() - t
		t = time.time()
		ball[0] += vector[0] * dt
		ball[1] += vector[1] * dt
		if ball[0] < 0.5:#yläreuna
			ball[0] = (ball[0] - 0.5) * -1 + 0.5
			vector[0] *= -1
		if ball[0] > 23.5:#alareuna
			ball[0] = (ball[0] - 23.5) * -1 + 23.5
			vector[0] *= -1
		if ball[1] < 0.5:#vasen reuna
			ball[1] = (ball[1] - 0.5) * -1 + 0.5
			vector[1] *= -1
		if ball[1] > 79.5:#oikea reuna
			ball[1] = (ball[1] - 79.5) * -1 + 79.5
			vector[1] *= -1
		scr.erase()
		try:
			scr.addstr(int(ball[0]), int(ball[1]), "O")
		except:
			pass
		scr.addstr(0, 0, "FPS: "+str(int(1/dt)))
		scr.refresh()

if __name__ == "__main__":
	sys.exit(wrapper(main))
